<template>
  <view class="u-wrap">
    <u-navbar :border-bottom="false" :is-back="false" title="" :background="{backgroundColor: 'rgba(0,0,0,0)'}">
      <view class="navbars u-f-ac u-f-jsb">
        <view class="u-f1">
          <u-icon @click="$u.route({type: 'back', delta: 1})" name="arrow-left" size="34" color="#fff"></u-icon>
        </view>
        <view class="u-f-ajc">
          <image src="/static/images/header.png" mode="widthFix"></image>
        </view>
        <view class="u-f1" style="text-align: right;">
          <!-- <u-button type="error" size="mini" :custom-style="customStyle" @click="showPopup=true">汇购预排</u-button> -->
        </view>
      </view>
    </u-navbar>

    <view class="num_box f-w-700 u-f-ajc u-f-column">
      <view>商品分类</view>
    </view>

    <view class="u-menu-wrap message-container">
      <scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
        <view v-for="(item,index) in tabbar" :key="index" class="u-tab-item"
          :class="[current == index ? 'u-tab-item-active' : '']" @tap.stop="swichMenu(index)">
          <text class="u-line-1">{{item.name}}</text>
        </view>
      </scroll-view>
      <block v-for="(item , index) in tabbar" :key="index">
        <scroll-view scroll-y class="right-box" v-if="current == index">
          <view class="page-view">
            <view class="class-item">
              <view class="two-class-item" v-for="(towItem, towIndex) in item.children" :key="towIndex"
                @click="jumpCatList(towItem.category_id)">
                <view class="item-title">
                  <view class="u-f-ajc u-f-column">
                    <u-image width="80rpx" height="80rpx" :src="towItem.image"></u-image>
                    <text>{{towItem.name}}</text>
                  </view>
                </view>
                <!-- <view class="item-container">
                	                <view class="thumb-box" v-for="(threeItem, threeIndex) in towItem.children" :key="threeIndex" @click.stop="jumpCatList(threeItem.category_id)">
                                        <u-image  width="80rpx" height="80rpx":src="threeItem.image"></u-image>
                	                    <view class="item-menu-name">{{threeItem.name}}</view>
                	                </view>
                	            </view>
                                <view v-if="towItem.children.length==0" style="text-align: center;font-size: 24rpx;">
                                    <text>无子分类</text>
                                </view> -->
              </view>
            </view>
          </view>
        </scroll-view>
      </block>
    </view>
    <u-no-network></u-no-network>
  </view>
</template>
<script>
  import * as API_Goods from '@/api/goods.js';

  export default {
    data() {
      return {
        // #ifdef MP
        background: {
          background: '#FD5B00'
        },
        // #endif
        // #ifndef MP
        background: {
          background: 'url(../../../static/images/bg/bg2.png) no-repeat',
          backgroundSize: 'cover'
        },
        // #endif
        scrollTop: 0, //tab标题的滚动条位置
        current: 0, // 预设当前项的值
        menuHeight: 0, // 左边菜单的高度
        menuItemHeight: 0, // 左边菜单item的高度
        tabbar: [],
        networkType: '' //网络状态
      }
    },
    onLoad() {
      let $this = this
      //获取网络类型
      uni.getNetworkType({
        success(res) {
          $this.networkType = res.networkType
          if (res.networkType !== 'none') {
            // 调用getCategory接口
            $this.getCategory(0);
          }
        }
      })
    },
    onShow() {
      let $this = this
      //监听网络状态变化
      uni.onNetworkStatusChange(res => {
        //监听到有网络并且第一次没有加载数据的情况下初始化数据
        if (res.isConnected && $this.networkType === 'none') {
          $this.getCategory(0);
        }
      })
    },
    onPullDownRefresh() {

      //下拉刷新重新获取数据
      let $this = this
      API_Goods.getCategory().then(res => {
        $this.tabbar = res;
        uni.stopPullDownRefresh();

      });

    },
    // onBackPress() {
    //   let strTime = new Date().getTime()
    //   if (strTime - this.strTime <= 2000) {
    //     console.log('连续两次返回 就是退出')
    //     this.onLogout()
    //   }
    //   this.strTime = strTime
    // },
    methods: {
      //退出登录
      onLogout() {
        uni.$emit('logout', {
          msg: '退出登录消息'
        })
        let mobile = uni.getStorageSync('mobile')
        //清除会员信息
        uni.removeStorageSync('user')
        //清除token
        uni.removeStorageSync('accessToken')
        uni.setStorageSync('mobile', mobile)
      },
      getCategory(id) {
        API_Goods.getCategory().then(res => {
          //小程序发布需过滤医药及食品类目
          // const data = res.filter(key=> key.category_id != 85 && key.category_id != 4)
          this.tabbar = res;
        });
      },
      // 点击左边的栏目切换1
      async swichMenu(index) {
        if (index == this.current) return;
        this.current = index;
        // 如果为0，意味着尚未初始化
        if (this.menuHeight == 0 || this.menuItemHeight == 0) {
          await this.getElRect('menu-scroll-view', 'menuHeight');
          await this.getElRect('u-tab-item', 'menuItemHeight');
        }
        // 将菜单菜单活动item垂直居中
        this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
      },
      // 获取一个目标元素的高度
      getElRect(elClass, dataVal) {
        new Promise((resolve, reject) => {
          const query = uni.createSelectorQuery().in(this);
          query.select('.' + elClass).fields({
            size: true
          }, res => {
            // 如果节点尚未生成，res值为null，循环调用执行
            if (!res) {
              setTimeout(() => {
                this.getElRect(elClass);
              }, 10);
              return;
            }
            this[dataVal] = res.height;
          }).exec();
        })
      },
      //TODO 点击跳转到搜索列表页
      jumpCatList(catId) {
        uni.navigateTo({
          url: `/goods-module/goods-list?cat_id=&category=${catId}&title=${this.tabbar[this.current].name}`
        });
      }
    }
  }
</script>

<style lang="scss" scoped>
  .u-wrap {
    height: calc(100vh);
    /* #ifdef H5 */
    height: calc(100vh - var(--window-top));
    /* #endif */
    display: flex;
    flex-direction: column;
  }

  .u-menu-wrap {
    flex: 1;
    display: flex;
    overflow: hidden;
  }

  .u-tab-view {
    width: 200rpx;
    height: 100%;
  }

  .u-tab-item {
    height: 110rpx;
    background: #f6f6f6;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 26rpx;
    color: #444;
    font-weight: 400;
    line-height: 1;
  }

  .u-tab-item-active {
    position: relative;
    color: #000;
    font-size: 30rpx;
    font-weight: 600;
    background: #fff;
  }

  .u-tab-item-active::before {
    content: "";
    position: absolute;
    border-left: 4px solid $u-type-error;
    height: 32rpx;
    left: 0;
    top: 39rpx;
  }

  .u-tab-view {
    height: 100%;
  }

  .right-box {
    background-color: rgb(250, 250, 250);
  }

  .page-view {
    padding: 16rpx;
  }

  .class-item {
    .two-class-item {
      background: url(/static/images/category/bg.png) no-repeat 0 0;
      background-size: cover;
      // background-color: #fff;
      margin-bottom: 20rpx;
      padding: 16rpx;
      // border-radius: 8rpx;
    }
  }

  .class-item:last-child {
    // min-height: 100vh;
  }

  .item-title {
    font-size: 26rpx;
    color: $u-main-color;
    font-weight: bold;

    >text {
      margin-top: 10rpx;
    }
  }

  .item-menu-name {
    font-weight: normal;
    font-size: 24rpx;
    color: $u-main-color;
    overflow: hidden;
    width: 150rpx;
    height: 35rpx;
    text-align: center;
  }

  .item-container {
    display: flex;
    flex-wrap: wrap;
  }

  .thumb-box {
    width: 33.333333%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    margin-top: 20rpx;
  }

  .item-menu-image {
    width: 120rpx;
    height: 120rpx;
  }
</style>


<style lang="scss">
  page {
    width: 100%;
    background: url(/static/images/bg.png) no-repeat 0 0;
    background-size: cover;
  }

  .num_box {
    padding: 30rpx 0 60rpx;
    color: #FFDDAE;

    >view:nth-of-type(1) {
      font-size: 60rpx;
    }
  }

  .message-container {
    background-color: #F4F5F7;
    border-radius: 40rpx 40rpx 0 0;
    padding: 50rpx 0 0;
  }
</style>